﻿package mortal.game.view.common.pageSelect
{

    public class PageTool extends Object
    {
        private var _array:Array;
        private var _total:int;
        private var _pagesCount:int;
        private var _limit:int;
        private var _currentPage:int = 1;

        public function PageTool(param1:Array, param2:int)
        {
            this._array = [];
            this.array = param1;
            this._limit = param2;
            return;
        }// end function

        public function getPrevPage(param1:int) : Array
        {
            var _loc_2:Array = null;
            this._currentPage = param1;
            if (this._currentPage <= 2)
            {
                _loc_2 = this.getByPageNo(1);
            }
            else if (this._currentPage <= this.pagesCount)
            {
                _loc_2 = this.getByPageNo((this._currentPage - 1));
            }
            else
            {
                _loc_2 = this.getByPageNo(this.pagesCount);
            }
            return _loc_2;
        }// end function

        public function getNextPage(param1:int) : Array
        {
            var _loc_2:Array = null;
            this._currentPage = param1;
            if ((this._currentPage + 1) <= this.pagesCount)
            {
                _loc_2 = this.getByPageNo((this._currentPage + 1));
            }
            else
            {
                _loc_2 = this.getByPageNo(this.pagesCount);
            }
            return _loc_2;
        }// end function

        public function getByPageNo(param1:int, param2:Boolean = true) : Array
        {
            var _loc_6:int = 0;
            var _loc_3:Array = [];
            var _loc_4:Number = 0;
            var _loc_5:Number = 0;
            if (param1 > 0)
            {
                _loc_4 = this.getStart(param1);
                _loc_5 = this.getEnd(param1);
                _loc_6 = _loc_4;
                while (_loc_6 <= _loc_5)
                {
                    
                    if (this.array[_loc_6] != undefined && param2 || param2 == false)
                    {
                        _loc_3.push(this.array[_loc_6]);
                    }
                    _loc_6++;
                }
            }
            this.autoSetCurrentPage(param1);
            return _loc_3;
        }// end function

        public function autoSetCurrentPage(param1:int) : void
        {
            if (param1 <= 1)
            {
                this._currentPage = 1;
            }
            else if (param1 > 1 && param1 <= this.pagesCount)
            {
                this._currentPage = param1;
            }
            else
            {
                this._currentPage = this.pagesCount;
            }
            return;
        }// end function

        public function getStart(param1:int) : int
        {
            var _loc_2:int = 0;
            if (param1 > 1)
            {
                _loc_2 = (param1 - 1) * this.limit;
            }
            return _loc_2;
        }// end function

        private function getEnd(param1:int) : int
        {
            var _loc_2:int = 0;
            if (param1 == 1)
            {
                if (this._total < this.limit)
                {
                    _loc_2 = this.total - 1;
                }
                else
                {
                    _loc_2 = this.limit - 1;
                }
            }
            else if (param1 < this.pagesCount)
            {
                _loc_2 = this.getStart(param1) + this.limit - 1;
            }
            else
            {
                _loc_2 = this.total - 1;
            }
            return _loc_2;
        }// end function

        public function get array() : Array
        {
            return this._array;
        }// end function

        public function set array(param1:Array) : void
        {
            if (param1)
            {
                this._array = param1;
                this._total = this._array.length;
            }
            return;
        }// end function

        public function get total() : Number
        {
            return this._total;
        }// end function

        public function set total(param1:Number) : void
        {
            this._total = param1;
            return;
        }// end function

        public function get pagesCount() : int
        {
            if (this.total == 0)
            {
                return 0;
            }
            if (this.total % this.limit == 0)
            {
                return this.total / this.limit;
            }
            return this.total / this.limit + 1;
        }// end function

        public function getPrevStartIndex(param1:int) : int
        {
            var _loc_2:int = 0;
            this._currentPage = param1;
            if (this._currentPage <= 2)
            {
                _loc_2 = this.getStart(1);
                this.autoSetCurrentPage(1);
            }
            else if (this._currentPage <= this.pagesCount)
            {
                _loc_2 = this.getStart((this._currentPage - 1));
                this.autoSetCurrentPage((this._currentPage - 1));
            }
            else
            {
                _loc_2 = this.getStart(this.pagesCount);
                this.autoSetCurrentPage(this.pagesCount);
            }
            return _loc_2;
        }// end function

        public function getNextStartIndex(param1:int) : int
        {
            var _loc_2:int = 0;
            this._currentPage = param1;
            if ((this._currentPage + 1) <= this.pagesCount)
            {
                _loc_2 = this.getStart((this._currentPage + 1));
                this.autoSetCurrentPage((this._currentPage + 1));
            }
            else
            {
                _loc_2 = this.getStart(this.pagesCount);
                this.autoSetCurrentPage(this.pagesCount);
            }
            return _loc_2;
        }// end function

        public function set pagesCount(param1:int) : void
        {
            this._pagesCount = param1;
            return;
        }// end function

        public function get limit() : int
        {
            return this._limit;
        }// end function

        public function set limit(param1:int) : void
        {
            this._limit = param1;
            return;
        }// end function

        public function get currentPage() : int
        {
            return this._currentPage;
        }// end function

        public function set currentPage(param1:int) : void
        {
            this._currentPage = param1;
            return;
        }// end function

    }
}
